package com.gjy.test.other;

import org.junit.jupiter.api.Test;

import java.util.concurrent.*;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-02-17 10:58:58
 */
public class T8 {

    private static final Integer PROCESSOR = Runtime.getRuntime().availableProcessors();

    @Test
    public void test4() {
        Runnable r = () -> {
            Class<? extends T8> clazz = this.getClass();
            System.out.println("clazz = " + clazz);
            System.out.println(Thread.currentThread().getName());

            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            for (StackTraceElement element : stackTrace) {
                System.out.println("element = " + element);
            }
        };

        new Thread(r).start();
    }

    @Test
    public void test3() {

        Semaphore semaphore = new Semaphore(100);
        ExecutorService pool = Executors.newFixedThreadPool(100);

        for (int i = 0; i < 100; i++) {
            pool.submit(() -> {
                try {
                    System.out.println("Hello World");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    semaphore.release();
                }
            });
        }

        System.out.println("AAA");
    }

    @Test
    public void test1() {
        CyclicBarrier barrier = new CyclicBarrier(100, () -> System.out.println("WORLD"));
//        ExecutorService pool = Executors.newCachedThreadPool();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(100, 100 * 2, 1, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        for (int i = 0; i < 100; i++) {
            pool.submit(() -> {
                try {
                    System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
                    System.out.println("Hello World");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    try {
                        barrier.await();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        System.out.println("AAA");
    }

    @Test
    public void test2() {

        int s = 1000;

        CountDownLatch cdl = new CountDownLatch(s);
        ExecutorService pool = Executors.newFixedThreadPool(10);
//        ExecutorService pool = Executors.newCachedThreadPool();

        for (int i = 0; i < s; i++) {
            pool.execute(() -> {
                try {
                    System.out.println("Hello World");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                } finally {
                    cdl.countDown();
                }
            });
        }

        try {
            cdl.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("world");
    }
}
